package com.lik888.web1.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import com.lik888.web1.entity.UserLogin;
import com.lik888.web1.entity.UserProductiontable;
import com.lik888.web1.entity.UserTable;
import com.lik888.web1.entity.UserproducionConnectiontable;
import com.lik888.web1.mapper.UserProductiontableMapper;
import com.lik888.web1.mapper.UserTableMapper;
import com.lik888.web1.mapper.UserproducionConnectiontableMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class UserDetailsServiceImpl  implements UserDetailsService {
    private final StringRedisTemplate stringRedisTemplate;
    private final UserTableMapper userTableMapper;
    private final UserProductiontableMapper userProductiontableMapper;
    private final UserproducionConnectiontableMapper userproducionConnectiontableMapper;
    ObjectMapper objectMapper = new ObjectMapper();

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        System.out.println(username.length());
        System.out.println(username);
        UserTable user = userTableMapper.selectOne(new LambdaQueryWrapper<UserTable>().eq(UserTable::getUsername,username));
        if(user == null){
            throw new UsernameNotFoundException("用户不存在");
        }
        List<String> list = new ArrayList<>();
        //TODO 从数据库中获取权限
        //获取userid
        Long userid = user.getUserId();
        //查询中间表
        LambdaQueryWrapper<UserproducionConnectiontable> connectionQueryWrapper = new LambdaQueryWrapper<UserproducionConnectiontable>();
        connectionQueryWrapper.eq(UserproducionConnectiontable::getUserId, userid);
        List<UserproducionConnectiontable> connectionTables = userproducionConnectiontableMapper.selectList(connectionQueryWrapper);
        //获取权限id
        List<Integer> productionIds = connectionTables.stream().map(UserproducionConnectiontable::getProductionId).collect(Collectors.toList());
        //根据权限id查询权限
        //判断是否有权限
        if(productionIds.isEmpty()){
            throw new RuntimeException("user has no permission");
        }
        LambdaQueryWrapper<UserProductiontable> productionQueryWrapper = new LambdaQueryWrapper<UserProductiontable>();
        productionQueryWrapper.in(UserProductiontable::getProductionId, productionIds);
        List<UserProductiontable> productionTables = userProductiontableMapper.selectList(productionQueryWrapper);
        //判断是否有权限
        if(productionTables.isEmpty()){
            throw new RuntimeException("user has no permission");
        }
        UserLogin login = new UserLogin(user, list);
        return login;
    }
}
